<?php
namespace App\Admin\Controllers\System\Traits;

use App\Wen\Utils\Settings;
use App\Wen\Utils\StrUtils;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use ZipArchive;

trait desiaiPcClientTrait{

    public function desai_init_var(){
        global $__MINI_GLOBAL_PAYCODE_28__;
        if(!$__MINI_GLOBAL_PAYCODE_28__){
            exit('您还没有获得 28号 模块的授权');
        }

        $this->main_color = strtolower(Settings::get('uni_color_primary', '#FC3A72'));
        $this->domain = str_replace(['https://', 'http://'], '', env('APP_URL', ''));
        $this->coins_name = Settings::get('app_coin_name', '硬币');
    }

    public function desai_pc(Request $request){
        $this->desai_init_var();
        $folderPath = base_path('third/DESAI/pc'); // 你想要打包的文件夹的路径
        // 创建一个临时文件路径用于存储生成的Zip文件
        $zipFileName = 'minisns_desiai_pc_'.get_mini_version_code().'.zip';
        $zipFilePath = storage_path('app/' . $zipFileName);

        // 创建一个ZipArchive对象并打开临时文件供写入
        $zip = new ZipArchive;
        $zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE);

        // 获取文件夹中的所有文件和目录
        $files = File::allFiles($folderPath);

        // 将文件夹中的每个文件添加到Zip文件中
        foreach ($files as $file) {
            $file_name = $file->getFilename();
            $local_name = $file->getRelativePath().'/'.$file_name;

            if(!$this->desai_filter($local_name)){
                continue;
            }
            $content = File::get($file);

            $this->desai_replaceProcess($content, $file_name, $local_name);
            $this->desai_additionProcee($content, $file_name, $local_name);

            $zip->addFromString($local_name, $content);
            if(strpos($local_name, 'pages/tabbar/') !== false){
                $zip->addFromString(str_replace('/tabbar/', '/tabbar_clone/', $local_name), $content);
            }
        }

//        $zip->addFromString('/index.html', $this->desai_get_index_html());

        // 添加图标
//        if(file_exists(public_path('storage/logos/'))){
//            $icon_files = File::allFiles(public_path('storage/logos/'));
//            foreach ($icon_files as $icon) {
//                $icon_name = $icon->getFilename();
//                $zip->addFromString('unpackage/res/icons/'.$icon_name, File::get($icon));
//            }
//        }

        // 关闭ZipArchive对象
        $zip->close();

        // 将生成的Zip文件返回供下载
        return response()->download($zipFilePath, $zipFileName)->deleteFileAfterSend();
    }

    public function desai_filter($local_name){
        if(in_array($local_name, ['/favicon.ico'])){
            return false;
        }
        return true;
    }

    public function desai_replaceProcess(&$content, $file_name, $local_name){
        $content = str_replace('硬币', $this->coins_name, $content);
        $content = str_replace('mini.minisns.cn', $this->domain, $content);


        if($local_name == '/index.html'){
            $content = str_replace('https://mini.h5.minisns.cn', Settings::get('app_h5_home', ''), $content);
            $content = str_replace('https://img.minisns.cn/desai/logo.png', Settings::get('desai_pc_logo', 'https://img.minisns.cn/desai/logo.png'), $content);
            $content = preg_replace('$<link rel="icon" href="[^"]*" />$', '<link rel="icon" href="'.Settings::get('desai_pc_icon', '').'" />', $content);
            $content = preg_replace('$<title>[^<]*</title>$', '<title>'.Settings::get('desai_pc_page_title', '').'</title>', $content);
            $content = preg_replace('$<meta name="keywords" content="[^"]*"\s?/?>$', '<meta name="keywords" content="'.Settings::get('desai_pc_page_keyword', '').'">', $content);
            $content = preg_replace('$<meta name="description" content="[^"]*"\s?/?>$', '<meta name="description" content="'.Settings::get('desai_pc_page_desc', '').'">', $content);
        }else if(StrUtils::startsWith($file_name, 'communityRight')){
            $bottom_qrcode_arr = Settings::get('desai_pc_right_bottom_qrcode', [1,2]);
            if(count($bottom_qrcode_arr) >= 2){

                if($bottom_qrcode_arr[0] == 1){
                    $content = str_replace('https://img.minisns.cn/qrcode/minipro.jpg', Settings::get('app_mini_code', 'https://img.minisns.cn/qrcode/minipro.jpg'), $content);
                } else if($bottom_qrcode_arr[0] == 2){
                    $content = str_replace(' 小程序 ', '公众号', $content);
                    $content = str_replace('https://img.minisns.cn/qrcode/minipro.jpg', Settings::get('app_mp_code', 'https://img.minisns.cn/pc/gzh_code.jpg'), $content);
                }else if($bottom_qrcode_arr[0] == 3){
                    $content = str_replace(' 小程序 ', 'App', $content);
                    $content = str_replace('https://img.minisns.cn/qrcode/minipro.jpg', Settings::get('app_app_code', 'https://img.minisns.cn/qrcode/app.png'), $content);
                }else if($bottom_qrcode_arr[0] == 4){
                    $content = str_replace(' 小程序 ', '抖音', $content);
                    $content = str_replace('https://img.minisns.cn/qrcode/minipro.jpg', Settings::get('app_douyin_code', 'https://img.minisns.cn/qrcode/app.png'), $content);
                }

                if($bottom_qrcode_arr[1] == 1){
                    $content = str_replace(' 公众号 ', '小程序', $content);
                    $content = str_replace('https://img.minisns.cn/pc/gzh_code.jpg', Settings::get('app_mini_code', 'https://img.minisns.cn/qrcode/minipro.jpg'), $content);
                }else if($bottom_qrcode_arr[1] == 2){
                    $content = str_replace('https://img.minisns.cn/pc/gzh_code.jpg', Settings::get('app_mp_code', 'https://img.minisns.cn/pc/gzh_code.jpg'), $content);
                }else if($bottom_qrcode_arr[1] == 3){
                    $content = str_replace(' 小程序 ', 'App', $content);
                    $content = str_replace('https://img.minisns.cn/pc/gzh_code.jpg', Settings::get('app_app_code', 'https://img.minisns.cn/qrcode/app.png'), $content);
                }else if($bottom_qrcode_arr[1] == 4){
                    $content = str_replace(' 小程序 ', '抖音', $content);
                    $content = str_replace('https://img.minisns.cn/pc/gzh_code.jpg', Settings::get('app_douyin_code', 'https://img.minisns.cn/qrcode/app.png'), $content);
                }
            }
        }else if(StrUtils::startsWith($file_name, 'communitywecome')){
            $content = str_replace('欢迎来到迷你SNS演示社区-宠也', Settings::get('desai_pc_page_welcome', '欢迎来到迷你SNS演示社区-宠也'), $content);
        }else if(StrUtils::startsWith($file_name, 'index')){
            if(!_empty_(env('APP_SAFE_CODE'))){
                $content = str_replace('headers.safeCode=""', 'headers.safeCode="'.env('APP_SAFE_CODE').'"', $content);
            }
            $content = str_replace('粤ICP备2022011211号-1', Settings::get('about_icp', ''), $content);
            $content = str_replace('京公网安备11010802020088号', Settings::get('about_gwab', ''), $content);
            $content = str_replace('https://mini.minisns.cn', env('APP_URL', ''), $content);

        }else if(StrUtils::startsWith($file_name, 'accoutSetting')){
            $content = str_replace('设计师', Settings::get('about_user_called', '铲屎官'), $content);
            if(Settings::get('desai_pc_app_order_pay_provider', 'wxpay') != 'wxpay'){
                $content = str_replace('wxpay', Settings::get('desai_pc_app_order_pay_provider', 'wxpay'), $content);
            }
            if(Settings::get('desai_pc_app_order_pay_type', 'code') != 'code'){
                $content = str_replace('code_pc', Settings::get('desai_pc_app_order_pay_type', 'code'), $content);
            }

        }
    }

    public function desai_additionProcee(&$content, $file_name, $local_name){
        if($local_name == '/app.scss'){
//            $content .= Settings::get('app_custom_css_app_scss', '');
        }
    }

}
